package com.hmsm.sys.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hmsm.sys.po.Menu;
import com.hmsm.sys.po.UserInfo;
import com.lzyyj.common.Page;
import com.lzyyj.gmis.base.dao.IBaseDao;
import com.lzyyj.gmis.base.service.BaseService;

@SuppressWarnings("serial")
@Service
public class MenuService extends BaseService<Menu, Integer> implements IMenuService<UserInfo, Menu> {

	@Override
	public Page<Menu> manage(Page<Menu> page) throws Exception {
		Page<Menu> result = null;
		
		Menu example = page.getFindByExample();
		if (example==null){
			example=new Menu();
			page.setFindByExample(example);
		}
		String exampleProperties = (String)page.getCondition().get(IBaseDao.EXAMPLE_PROPERTIES);
		if (exampleProperties==null||exampleProperties.length()==0){
			Menu root = getMenuByName(DEFAULT_MENU_TYPE);
			example.setParent(root);
			page.getCondition().put(IBaseDao.EXAMPLE_PROPERTIES, "parent");
		}
		
		String orderFields = page.getOrderFields();
		if (orderFields==null||orderFields.length()==0){
			orderFields="parent,orderNo";
			page.setOrderFields(orderFields);
		}
		result = super.manage(page);
		List<Menu> list = new ArrayList<Menu>();
		
		return result;
	}

	private static final Log log = LogFactory.getLog(MenuService.class);
	
	public static final String DEFAULT_MENU_TYPE = "系统主菜单";
	
	@Resource
	private IUserInfoService<UserInfo, Integer> userService;
	
	public IUserInfoService<UserInfo, Integer> getUserService() {
		return userService;
	}
	
	public void setUserService(IUserInfoService<UserInfo, Integer> userService) {
		this.userService = userService;
	}

	@Resource
	private RoleInfoService roleInfoService;

	public RoleInfoService getRoleInfoService() {
		return roleInfoService;
	}

	public void setRoleInfoService(RoleInfoService roleInfoService) {
		this.roleInfoService = roleInfoService;
	}

	public List<Menu> getMainMenus(UserInfo currentUser) throws Exception{
		return getMenusBy(DEFAULT_MENU_TYPE, currentUser);
	}

	public List<Menu> getSystemTools(UserInfo currentUser) throws Exception{
		return getMenusBy("系统工具栏", currentUser);
	}
	
	/**
	 * 获取指定名称的菜单，仅限于根菜单
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public Menu getMenuByName(String name) throws Exception{
		Menu menu = null;
		
		if (name!=null&&name.length()>0){
			Menu example = new Menu();
			example.setName(name);
			List<Menu> menus = findByExample(example, "parent,=name", "orderNo", 0,1);
			if (menus.size()>0){
				log.debug("找到指定菜单类型！");
				menu = menus.get(0);
			}
		}
		
		return menu;
	}

		
	/**
	 * 获取指定类型的菜单，缺省值“系统主菜单”
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public List<Menu> getMenusBy(String name, UserInfo currentUser) throws Exception{
		List<Menu> mainMenus = new ArrayList<Menu>();
		
		if (name==null||name.length()==0){
			name = DEFAULT_MENU_TYPE;
		}
		Menu menu = getMenuByName(name);
		if (menu!=null){
			List<Menu> menus = menu.getMenus();
			log.debug("获取一级菜单："+menus.size()+"项");
			// 遍历一级菜单
			for(Menu mainMenu:menus){
				menu = new Menu();
				menu.setId(mainMenu.getId());
				menu.setOrderNo(mainMenu.getOrderNo());
				menu.setName(mainMenu.getName());
				menu.setDescription(mainMenu.getDescription());
				menu.setUrl(mainMenu.getUrl());
				menu.setIcon(mainMenu.getIcon());
				menu.setBackgroundImage(mainMenu.getBackgroundImage());
				menu.setMenuImage(mainMenu.getMenuImage());
				
				log.debug("获取二级子菜单："+mainMenu.getMenus().size());
				for(Menu subMenu:mainMenu.getMenus()){
					// 权限检查
					subMenu.setShowEnabled(subMenu.getPurviewKey()==null||currentUser!=null&& getRoleInfoService().haveRight(subMenu.getPurviewKey(), currentUser));
					// 有权限的话添加到菜单列表
					if (subMenu.isShowEnabled()){
						log.debug("添加菜单项："+subMenu.getName());
						menu.getMenus().add(subMenu);
					}
				}
				// 如果有子菜单项才添加到主菜单列表
				if (menu.getMenus().size()>0){
					log.debug("添加主菜单项："+menu);
					mainMenus.add(menu);
				}
			}
		}
		
		return mainMenus;
	}

	public List<Menu> getShortcutMenus(UserInfo currentUser) throws Exception {
		return getMenusBy("办公系统快捷菜单", currentUser);
	}

	public List<Menu> getOaMenus(UserInfo currentUser) throws Exception {
		return getMenusBy("办公系统菜单", currentUser);
	}
	
	public Page<Menu> manage(Page<Menu> page, UserInfo currentUser){
		Menu example = page.getFindByExample();
		if (example==null){
			example = new Menu();
			page.setFindByExample(example);
		}
		String exampPropert = "parent";
		page.getCondition().put(IBaseDao.EXAMPLE_PROPERTIES, exampPropert);
		
		Page<Menu> pagePo = getBaseDao().find(page);
		return pagePo;
	}

	@Override
	public Menu save(Menu menuVo, UserInfo currentUser) throws Exception {
		Menu menuPo = null;
		
		if (menuVo.getId()!=null&&menuVo.getId()>0){
			menuPo = get(menuVo);
		}
		String propertyList = "parent,name,url,description,purviewKey";
		if(menuPo!=null){
			propertyList = "name,url,description,purviewKey";
			copyProperties(menuVo, menuPo, propertyList, false);
			update(menuPo, propertyList);
		}else{
			Menu parent = menuVo.getParent();
			if(parent!=null&&parent.getId()!=null){
				parent = get(parent);
				if(parent==null){
					throw new Exception("指定的菜单类型或功能模块不存在或已删除！");
				}
			}
			if(parent!=null&&parent.getLevel()>2){
				throw new Exception("菜单下不能再建菜单项！");
			}
			propertyList = "parent,name,url,description,purviewKey";
			menuPo = save(menuVo, propertyList);
		}
		
		return menuPo;
	}

	@Override
	public void del(Menu menuVo, UserInfo currentUser) throws Exception {
		Menu menuPo = null;
		
		if (menuVo.getId()!=null){
			menuPo = this.get(menuVo.getId());
			if(menuPo!=null){
				super.del(menuVo.getId());
			}
		}
		
	}

}
